import store from '../store'
export default{
    install (Vue) {
        //这是根据你创建空间时所选择的地区
        const uploadUrl = 'https://upload-z2.qiniup.com'
        // 上传文件到七牛云
        const config = {
            headers: {'Content-Type': 'multipart/form-data'},
            onUploadProgress: progressEvent => {
                // progressEvent.loaded:已上传文件大小
                // progressEvent.total:被上传文件的总大小
                let percentage = Number((progressEvent.loaded / progressEvent.total * 100).toFixed(2))
                console.info(progressEvent.loaded)
                console.info(progressEvent.total)
                if(percentage<=100){
                    store.commit('setUploading',
                    {status:true,percentage:percentage})
                }
            }
        }
        Vue.prototype.$qiniu = function (req) {
            let category = ''
            // 从后端获取上传凭证token
            this.$http.get('/upload').then(res => {

                switch (req.file.type) {
                    case 'image/jpeg':
                    case 'image/png':
                        category = 'picture'
                        break
                    case 'video/mpeg4':
                    case 'video/mp4':
                        category = 'video'
                        break
                    case 'application/x-zip-compressed':
                    case 'application/octet-stream':
                        category = 'compressed_file'
                        break
                    default:
                        category = 'file'
                        break
                }
        
                console.log(req.file.name)
                let datetime = new Date();
                let dt = datetime.toLocaleString();
                console.log(dt);
                // 重命名要上传的文件
                const keyname = category +'-'+dt+'-'+req.file.name
                console.log(keyname)

                const formdata = new FormData()
                formdata.append('file', req.file)
                formdata.append('token', res.data.upToken)
                formdata.append('key', keyname)
                console.log(res.data.message)
                const returnUrl = "http://file.szuet.ltd/"
                // 获取到凭证之后再将文件上传到七牛云空间
                this.$http.post(uploadUrl, formdata, config).then(res => {
                            switch(category){
                                case 'picture':
                                    console.log(this.$store.state.type)
                                    this.$store.commit('setCarouselPicture',returnUrl+res.data.key)
                                    if(this.$store.state.type=='certificate'){
                                        this.$store.commit('setFiles',
                                            {name:req.file.name, url:returnUrl+res.data.key,
                                            file_type:'certificate'})
                                    }
                                    else if(this.$store.state.type=='cover'){
                                        this.$store.commit('setFiles',
                                                {name:req.file.name, url:returnUrl+res.data.key,
                                                file_type:'cover'})
                                    }
                                    break
                                case 'file':
                                    this.$store.commit('setFiles',
                                    {name:req.file.name, url:returnUrl+res.data.key,file_type:category})
                                    break
                                case 'video':
                                    this.$store.commit('setFiles',
                                    {name:req.file.name, url:returnUrl+res.data.key,file_type:category})
                                    break
                                case 'compressed_file':
                                    this.$store.commit('setFiles',
                                    {name:req.file.name, url:returnUrl+res.data.key,file_type:category})
                                    break
                            }
                            store.commit('setUploading',
                            {status:false,percentage:config.percentage})
                            this.$message({
                                type:'success',
                                message:"上传成功！"
                            })
                        }).catch(e=>{
                            if(e.response.staus==='401'){
                                this.$message({
                                    type:'warning',
                                    message:"鉴权失败。"
                                })
                            }else if(e.response.status==='500'){
                                console.log('error 500')
                            }else if(e.response.status==='614'){
                                this.$message({
                                    type:'warning',
                                    message:"该文件名已存在！请重命名该文件！"
                                })
                            }
                            })
            })
        }
    },
}